-- spell_to_stoptime
-- created by xinj
-- 施放某系魔法时时间停止1轮

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,
    -- 触发
    trigger = function(who, prop, arg)
        if who.type ~= OBJECT_TYPE_USER then
            return;
        end

        -- 不是要触发的系别
        if arg ~= tonumber(prop[2]) then
            return;
        end

        -- BOSS层不触发
        local dungeonId = DungeonM.getDungeonId();
        local layer = DungeonM.currentLayer();
        if DungeonBossM.isBossLayer(dungeonId, layer, who) then
            return;
        end

        -- 已在时间停止状态，不再触发
        if CombatStatusM.checkStatus(who, "stop_action") then
            return;
        end

        local rand = DungeonM.getRandSeed("spell_to_stoptime") % 1000;
        if (rand > prop[3]) then
            return;
        end

        local round = CombatM.getRound();

        local condition = {
            ["end_round"] = round + prop[4] + 1,
        };

        -- 附加施法者的状态
        CombatStatusM.applyStatus(who, "stop_action", condition);

        -- 增加命中动作
        local skillId = 35;
        SkillM.initSequence(who, who, skillId);
        SkillM.getSequence(who):hit(who, who, skillId);

        EventMgr.fire(event.SPELL_STOP_TIME);

        -- 所有格子
        local grids = DungeonM.getCurrentDungeon();
        local ret = {};
        for _, grid in pairs(grids) do
            -- 没死的怪物，且没免疫
            if  grid:isMonster() and
                not grid.monster:isDead() and
                not (grid.monster.isAudience and grid.monster:isAudience()) then

                if not SkillM.immunitySkill(who, grid.monster, skillId) then
                    -- 附加状态
                    CombatStatusM.applyStatus(grid.monster, "stop_action", condition);

                    EventMgr.fire(event.GRID_LOCK_STATE, {["pos"] = grid:getPos(), });
                    -- 目标
                    table.insert(ret, grid.monster);
                else
                    -- 先初始化
                    SkillM.initSequence(who, grid.monster, skillId);

                    if FormulaM.invoke("CAN_TARGET_INJURED", grid.monster, skillId)
                        and grid:isOpened() then
                        -- 能受创并且已经翻开的
                        SkillM.getSequence(grid.monster):immunity(who, grid.monster, skillId);
                    end
                end
            end
        end
    end,

    record = function(target, prop, path)
        return false;
    end
};
